<?php

namespace App\Http\Controllers\Cruise\Merchant;

use App\Enums\Common\CommonEnum;
use App\Http\Controllers\BaseController;
use App\Models\Common\ResourcesModel;
use App\Models\Cruise\CruiseCheckDetailModel;
use App\Models\Cruise\CruiseCheckRectifyModel;
use App\Models\Cruise\CruiseCheckRectifyReplyModel;
use App\Models\Cruise\CruiseTemplateModel;
use App\Service\Cruise\Check\CruiseCheckRectifyService;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Validation\Rule;

class CruiseCheckRectifyController extends BaseController
{
    public function listsCount(Request $request): JsonResponse
    {
        $shopId   = (int)$request->header('shopId');
        $templateType = (int)$request->get('templateType', CruiseTemplateModel::TYPE_3);

        return $this->success([
            'agg' => collect(CruiseCheckRectifyModel::STATUS_TEXT)->map(function ($text, $status) use ($shopId, $templateType) {
                return [
                    'status' => $status,
                    'text'   => $text,
                    'count'  => CruiseCheckRectifyService::agg($shopId, $status, $templateType),
                ];
            })->values()
        ]);
    }

    public function lists(Request $request): JsonResponse
    {
        $pageNum  = (int)$request->get('pageNum');
        $pageSize = (int)$request->get('pageSize');
        $status   = (int)$request->get('status', CruiseCheckRectifyModel::STATUS_WAIT_TOTAL);
        $dealUid  = (int)$request->get('dealUid', 0);
        $shopId   = (int)$request->header('shopId');
        $templateType = (int)$request->get('templateType', CruiseTemplateModel::TYPE_3);

        $request->merge(['shopId' => $shopId]);

        $this->validate($request, [
            'shopId'   => 'required|integer|min:1',
            'status'   => 'integer',
            'pageNum'  => 'integer',
            'pageSize' => 'integer',
        ]);

        $this->validate($request, []);

        $currentUid = getRmsUserId();

        return $this->success(
            (new CruiseCheckRectifyService(0, $currentUid))->lists(null, $status, $pageSize, $pageNum, 0, 0, $shopId, $templateType, [], $dealUid)
        );
    }

    public function detail(Request $request, int $id): JsonResponse
    {
        $currentUid = getRmsUserId();

        return $this->success((new CruiseCheckRectifyService($id, $currentUid))->detail(CommonEnum::PLATFORM_MERCHANT));
    }

    public function submit(Request $request, int $id): JsonResponse
    {
        $files = $request->post('files');
        $desc  = $request->post('desc');

        $this->validate($request, [
            'files'        => 'required|array',
            'files.*.url'  => 'required|string',
            'files.*.type' => ['required', 'string', Rule::in(ResourcesModel::EXTRA_TYPES)],
            'desc'         => 'required|string',
        ]);

        $currentUid = getRmsUserId();

        (new CruiseCheckRectifyService($id, $currentUid))->submitReply($files, $desc);

        return $this->success();
    }

    public function replyDetail(Request $request, int $id)
    {
        return $this->success([
            'rectifyReply' => CruiseCheckRectifyReplyModel::getDataByIds([$id])[$id] ?? [],
        ]);
    }

    public function dealUidList(Request $request)
    {
        $this->validate($request, [
            'templateType' => 'required|integer',
        ]);

        return $this->success(CruiseCheckDetailModel::dealUidList($request->templateType));
    }
}
